package de.dlrg.wms.gui.views;

import java.util.List;


import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;

import de.dlrg.wms.controller.controllers.RunController;
import de.dlrg.wms.gui.FormContainer;
import de.dlrg.wms.gui.forms.RunForm;
import de.dlrg.wms.gui.forms.generic.AbstractTableViewWithDeleteEdit;
import de.dlrg.wms.gui.views.coulmnNames.RunColumnNames;
import de.dlrg.wms.model.Run;
import de.dlrg.wms.model.SwimmerTrackRun;

import static de.dlrg.wms.gui.views.coulmnNames.RunColumnNames.*;

public class RunView extends AbstractTableViewWithDeleteEdit {

	private RunController runController;

	public RunView(Composite arg0, int arg1, final RunController runController,
			final Shell shell) {
		super(arg0, arg1);
		this.tableColumns = RunColumnNames.class;
		
		this.runController = runController;
		this.initGUI(shell);

		Button btnAutoAssign = new Button(btnBar, SWT.NONE);
		btnAutoAssign.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				MessageBox messageBox = new MessageBox(shell,
						SWT.ICON_INFORMATION | SWT.OK | SWT.CANCEL);
				messageBox
						.setMessage("Are you sure? All current run assignments will be deleted");

				if (messageBox.open() == SWT.OK) {
					runController.autoAssignSwimmer();
					refresh();
				}
			}
		});
		btnAutoAssign.setText("Auto Assign");

	}

	@Override
	protected void refresh() {
		this.table.clearAll();
		this.table.setItemCount(0);
		List<Run> runs = this.runController.readAll();
		for (Run run : runs) {
			TableItem runTableItem = this.addSwimmerTrackRunData(run
					.getSwimmerTrackRun().get(0));
			runTableItem.setText(RUN_ID_COLUMN.ordinal(), "" + run.getId());
			runTableItem.setData(run);
			runTableItem.setText(RUN_NO_COLUMN.ordinal(), "" + run.getRunNo());

			for (int i = 1; i < run.getSwimmerTrackRun().size(); i++) {
				this.addSwimmerTrackRunData(run.getSwimmerTrackRun().get(i))
						.setData(run);
			}
		}
	}

	private TableItem addSwimmerTrackRunData(SwimmerTrackRun swimmerTrackRun) {
		TableItem swimmerTrackRunTableItem = new TableItem(this.table, SWT.NULL);
		{
			swimmerTrackRunTableItem.setText(AGE_CLASS_COLUMN.ordinal(),
					swimmerTrackRun.getSwimmer().getAgeClass().getName());
			swimmerTrackRunTableItem.setText(SWIMMER_COLUMN.ordinal(),
					swimmerTrackRun.getSwimmer().getName() + ", "
							+ swimmerTrackRun.getSwimmer().getSurName());
			swimmerTrackRunTableItem.setText(DISCIPLINE_COLUMN.ordinal(),
					swimmerTrackRun.getDiscipline().getName());
			swimmerTrackRunTableItem.setText(TRACK_COLUMN.ordinal(), ""
					+ swimmerTrackRun.getTrack().getTrackNumber());
			swimmerTrackRunTableItem.setText(TIME_COLUMN.ordinal(), ""
					+ swimmerTrackRun.getTime());
			swimmerTrackRunTableItem.setText(POINTS_COLUMN.ordinal(), ""
					+ swimmerTrackRun.getPoints());
		}
		return swimmerTrackRunTableItem;
	}

	@Override
	protected void editPressed() {
		Run selectedRun = (Run) table.getSelection()[0].getData();
		FormContainer formContainer = new FormContainer();
		formContainer.createContents();
		RunForm runForm = new RunForm(formContainer.getComposite(), SWT.BORDER,
				runController, selectedRun);
		runForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		runForm.getClosing().addObserver(formContainer);
		formContainer.open();
	}

	@Override
	protected void deletePressed() {

	}

//	@Override
//	protected String[] getColumnNames() {
//		String ret[] = new String[RunColumnNames.values().length];
//		for (RunColumnNames columnName : RunColumnNames.values()) {
//			ret[columnName.ordinal()] = columnName.toString();
//		}
//		return ret;
//	}

}
